package com.eight.cloud.rabbitmq.util;

import lombok.Data;

/**
 * @ClassName: SendStrategy
 * @Author: TXC
 * @Date: 2025-08-13 16:09
 **/
@Data
public class SendStrategy {
    private int maxRetries = 3;
    private long initialRetryDelay = 1000;
    private boolean exponentialBackoff = true;
    private boolean transactionAware = true;
    private RateLimiter rateLimiter;

    /**
     * @return 默认策略
     */
    public static SendStrategy defaultStrategy() {
        return new SendStrategy();
    }

    /**
     * @return 快速失败策略
     */
    public static SendStrategy failFast() {
        return new SendStrategy().maxRetries(0);
    }

    /**
     * @return 可靠发送策略
     */
    public static SendStrategy reliable() {
        return new SendStrategy()
                .maxRetries(5)
                .initialRetryDelay(2000);
    }

    /**
     * @return 异步策略
     */
    public static SendStrategy async() {
        return new SendStrategy().nonTransactional();
    }

    /**
     * @param permitsPerSecond 每秒许可数
     * @return 限流策略
     */
    public static SendStrategy rateLimited(double permitsPerSecond) {
        return new SendStrategy().rateLimiter(new RateLimiter(permitsPerSecond));
    }

    /**
     * @param maxRetries 最大重试次数
     * @return 最大重试次数策略
     */
    public SendStrategy maxRetries(int maxRetries) {
        this.maxRetries = maxRetries;
        return this;
    }

    /**
     * @param milliseconds 初始重试延迟（毫秒）
     * @return 初始重试延迟策略
     */
    public SendStrategy initialRetryDelay(long milliseconds) {
        this.initialRetryDelay = milliseconds;
        return this;
    }

    /**
     * @return 指数退避策略
     */
    public SendStrategy withExponentialBackoff() {
        this.exponentialBackoff = true;
        return this;
    }

    /**
     * @return 禁用指数退避策略
     */
    public SendStrategy withoutExponentialBackoff() {
        this.exponentialBackoff = false;
        return this;
    }

    /**
     * @return 事务感知策略
     */
    public SendStrategy transactionAware() {
        this.transactionAware = true;
        return this;
    }

    /**
     * @return 非事务感知策略
     */
    public SendStrategy nonTransactional() {
        this.transactionAware = false;
        return this;
    }

    /**
     * @param rateLimiter 速率限制器
     * @return 速率限制策略
     */
    public SendStrategy rateLimiter(RateLimiter rateLimiter) {
        this.rateLimiter = rateLimiter;
        return this;
    }
}
